<?php

namespace App\Models;

use Dcat\Admin\Models\MenuCache;
use Dcat\Admin\Traits\HasDateTimeFormatter;
use Dcat\Admin\Traits\ModelTree;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class Room extends Model
{
    use HasDateTimeFormatter,
        MenuCache,
        ModelTree {
        allNodes as treeAllNodes;
        ModelTree::boot as treeBoot;
    }

    protected $table = 'room';
    public $timestamps = false;
    protected $orderColumn = 'id';

    /**
     * 定义你的关联模型.
     *
     * @return BelongsToMany
     */
    public function user(): BelongsToMany
    {
        $pivotTable = 'room_user'; // 中间表

        $relatedModel = User::class; // 关联模型类名

        return $this->belongsToMany($relatedModel, $pivotTable, 'room_id', 'user_id');
    }


    /**
     * 创建时间
     * @return Attribute
     */
    protected function createTime(): Attribute
    {
        return Attribute::make(
            get: fn($value) => \date('Y-m-d H:i:s', $value),
        );

    }

    /**
     * 更新时间。
     *
     * @return Attribute
     */
    protected function updateTime(): Attribute
    {
        return Attribute::make(
            get: fn($value) => \date('Y-m-d H:i:s', $value),
        );
    }


}
